package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.ResumeAddDTO;
import com.oa.pm.dto.ResumeListParamDTO;
import com.oa.pm.entity.Resume;
import com.oa.pm.service.IResumeService;
import com.oa.pm.vo.ResumeDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/resume")
@Api(description = "简历库表模块")
public class ResumeController extends BaseController {

    private final IResumeService resumeService;

    @Autowired
    public ResumeController(IResumeService resumeService) {
        this.resumeService = resumeService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<List<Resume>> findList(ResumeListParamDTO resumeListParamDTO) {
        return new Resp().success(resumeService.findResumeList(resumeListParamDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public void export(ResumeListParamDTO resumeListParamDTO) {
        resumeService.export(resumeListParamDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<ResumeDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(resumeService.findResumeById(id));
    }


    /**
     * 新增
     *
     * @param resume Resume
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "简历库表对象") @RequestBody(required = false) ResumeAddDTO resume) {
        if (resumeService.addResume(resume)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param resume Resume
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "简历库表对象") @RequestBody(required = false) ResumeAddDTO resume) {
        if (resumeService.editResume(resume)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Resume resume = resumeService.selectById(id);
        if (resume != null && resume.getIsDeleted() == 2) {
            resume.setIsDeleted(1);
            if (resumeService.updateById(resume)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
